﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demo.Framework.DataStructure.Problems.AthleteTower
{
    class AthleteTower
    {
        private int _nunberOfAthletes;
        private List<Tuple<int, int>> _parameters;
        private List<Athlete> _athletes;
        private Athlete _firstAthlete;

        public AthleteTower(int nunberOfAthletes, List<Tuple<int, int>> parameters)
        {
            if (parameters == null)
            {
                throw new Exception("Athlete parameters can not be null");
            }
            _nunberOfAthletes = nunberOfAthletes;
            _parameters = parameters;
            _athletes = new List<Athlete>();
            _firstAthlete = null;
            Initialize();
        }

        private void Initialize()
        {
            try
            {
                // get the athlete with max strength
                // put him at the bottom
                // sort the athletes w.r.t mass on ascending
                // pile them up on the first athlete till total mass <= strength
                
                foreach (Tuple<int, int> item in _parameters)
                {
                    Athlete athelete = new Athlete(item.Item1, item.Item2);

                    if (_firstAthlete == null)
                    {
                        // 1st iteration
                        _firstAthlete = athelete;
                    }
                    else if (athelete.Strength > _firstAthlete.Strength)
                    {
                        _athletes.Add(_firstAthlete);
                        _firstAthlete = athelete;
                    }
                    else
                    {
                        _athletes.Add(athelete);
                    }                    
                }
                _athletes.Sort();
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        public int GetMaxTowerHeight()
        {
            try
            {   
                LinkedList<Athlete> atheleList = new LinkedList<Athlete>(_athletes);                
                LinkedListNode<Athlete> currentNode = atheleList.First;

                int maxTowerHeight = 1; // tower already contains first member

                for (int i = 0; i < atheleList.Count -1 && currentNode !=null; i++)
                {
                    if (currentNode.Previous != null)
                    {
                        // check if the current athlete can take the weight of the current tower
                        currentNode.Value.PutAthleteOnTop(currentNode.Previous.Value);
                        if (!currentNode.Value.TestStrength())
                        {
                            break;
                        }                                               
                    }

                    // check if the first athelete can take the weight of the tower
                    _firstAthlete.PutAthleteOnTop(currentNode.Value);
                    if (!_firstAthlete.TestStrength())
                    {
                        break;
                    }
                    maxTowerHeight++;
                    currentNode = currentNode.Next;                    
                }
                return maxTowerHeight;                
            }
            catch (Exception)
            {
                
                throw;
            }
        }
    }
}
